﻿using ArtMath.Core.Data;
using ArtMath.Core.Helper;
using ArtMath.Data;
using ArtMath.Resource;
using System;
using System.Drawing;
using ArtMath.CommonType;

namespace ArtMath.Component.CoordinateSystem
{
    /// <summary>
    /// Y轴
    /// </summary>
    [Serializable]
    public class YAxis : RectangularCoordinateAxisBase
    {
        public YAxis(SketchPad pad) : base(pad, Resources.Translate("YAxis"), ToolTypeName.yz, false)
        {
            GraduationTextLocation = AxisTextLocation.Above;
        }
        protected override void Scale_ParametersValueChanged(DocStringX sender, ParametersValueChangedEventArgs e)
        {
            base.Scale_ParametersValueChanged(sender, e);
            if (Scale.Value > 0)
            {
                Pad.Coordinate.CoordinateChanged(PointsRelocationReason.YAxisScaled);
            }
        }
        protected override void Scale_ExpressionSet(StringX sender, ExpressionSetEventArgs e)
        {
            base.Scale_ExpressionSet(sender, e);
            if (Scale.Value > 0)
            {
                Pad.Coordinate.CoordinateChanged(PointsRelocationReason.YAxisScaled);
            }
        }
        protected override string getFormula()
        {
            if (Pad.CoordinateType == CoordinateSystemType.PolarCoordinate)
            {
                return "ρcos(θ)=0";//可能永远无法执行
            }
            else
            {
                return "x=0";
            }
        }
        public override void CreateComponents()
        {
            Slope = double.PositiveInfinity;
            base.CreateComponents();
        }
        public override void Paint(Graphics gra)
        {
            if (ObjectState == ObjectState.Normal && Visible)
            {
                double gridAngle = Core.Helper.Angle.StandardizeRad(linkedRC.RotateAngle.RadValue),
                    axisAngle = (gridAngle + Core.Helper.Angle.RightAngle) % Core.Helper.Angle.RadRound;
                SolidBrush SB = new SolidBrush(StyleState_ == StyleState.Hover || Selected && Pad.UseReversedColor ? textColor.ReversedColor : textColor.Color);
                PointD loc = (linkedRC.OriginCoordinate + Pad.Location) / Pad.Zoom.ToPointD();
                SizeD size = new SizeD(Pad.Canvas.ClientSize.Width / Pad.Zoom.X.Value,
                    Pad.Canvas.ClientSize.Height / Pad.Zoom.Y.Value);
                PointD gridlen;
                Pen kdPen = new Pen(StyleState_ == StyleState.Hover || Selected && Pad.UseReversedColor ? rulerColor.ReversedColor : rulerColor.Color, 2),
                    gridPen = new Pen(StyleState_ == StyleState.Hover || Selected && Pad.UseReversedColor ? gridColor.ReversedColor : gridColor.Color, 1);
                if (!linkedRC.XAxis.Visible)
                {
                    if (ShowGrid)
                    {
                        gridlen = Utility.CalcLength(loc, size, gridAngle * Core.Helper.Angle.RToD);
                        gra.DrawLine(gridPen, (float)-gridlen.X, 0, (float)gridlen.Y, 0);
                    }
                    gra.DrawLine(kdPen, -6, 0, 6, 0);
                }
                PointD axislen = Utility.CalcLength(loc, size, axisAngle * Core.Helper.Angle.RToD);
                string ruler;
                double rulerCount = 0;
                while ((rulerCount + interval) * Scale.Value < axislen.Y - 17)
                {
                    rulerCount += interval;
                    gra.TranslateTransform(0, (float)(-interval * Scale.Value));
                    if (ShowGrid)
                    {
                        gridlen = Utility.CalcLength(loc.POffset(axisAngle, rulerCount * Scale.Value), size, gridAngle * Core.Helper.Angle.RToD);
                        gra.DrawLine(gridPen, (float)-gridlen.X, 0, (float)gridlen.Y, 0);
                    }
                    if (ShowGraduation)
                    {
                        if (!rulerCount.IsInteger()) kdPen.Width = 1;
                        gra.DrawLine(kdPen, -6, 0, 6, 0);//画刻度
                        if (!rulerCount.IsInteger()) kdPen.Width = 2;
                    }
                    if (ShowGraduationText)
                    {
                        ruler = FormatRuler(rulerCount);
                        gra.DrawString(ruler, new Font("Times New Roman", rulerCount.IsInteger() ? 16 : 10), SB,
                            (rulerCount.IsInteger() ? new PointF(GraduationTextLocation == AxisTextLocation.Above ? (-ruler.Length * 9) - 12 : 6, -11):
                            new PointF(GraduationTextLocation == AxisTextLocation.Above ? -((ruler.Length - 1) * 7 + 2) - 10 : 6, -7)));
                    }
                }
                Max = rulerCount * Unit.Value;
                gra.TranslateTransform(0, (float)(rulerCount * Scale.Value));//页坐标系恢复
                if (linkedRC.ShowxOy)
                    gra.DrawString("y", new Font("Times New Roman", 20, FontStyle.Italic),
                    SB, GraduationTextLocation == AxisTextLocation.Above ? 10 : -27, (float)(-6 - axislen.Y));
                //y轴负方向
                rulerCount = 0;
                while ((interval - rulerCount) * Scale.Value < axislen.X)
                {
                    rulerCount -= interval;
                    gra.TranslateTransform(0, (float)(interval * Scale.Value));
                    if (ShowGrid)
                    {
                        gridlen = Utility.CalcLength(loc.POffset(axisAngle, rulerCount * Scale.Value), size, gridAngle * Core.Helper.Angle.RToD);
                        gra.DrawLine(gridPen, (float)-gridlen.X, 0, (float)gridlen.Y, 0);
                    }
                    if (ShowGraduation)
                    {
                        if (!rulerCount.IsInteger()) kdPen.Width = 1;
                        gra.DrawLine(kdPen, -6, 0, 6, 0);//画刻度
                        if (!rulerCount.IsInteger()) kdPen.Width = 2;
                    }
                    if (ShowGraduationText)
                    {
                        ruler = FormatRuler(rulerCount);
                        gra.DrawString(ruler, new Font("Times New Roman", rulerCount.IsInteger() ? 16 : 10), SB,
                            (rulerCount.IsInteger() ? new PointF(GraduationTextLocation == AxisTextLocation.Above ? (-ruler.Length * 9 + 2) - 12 : 7, -11) :
                            new PointF(GraduationTextLocation == AxisTextLocation.Above ? -((ruler.Length - 2) * 7 + 4) - 12 : 7, -7)));
                    }
                }
                Min = rulerCount * Unit.Value;
                gra.TranslateTransform(0, (float)(rulerCount * Scale.Value));
                gra.DrawLine(LinePen, 0, (float)axislen.X, 0, (float)-axislen.Y);
            }
            //base.Paint(gra);
        }
    }
}
